<?php

/**
 * Filmx model
 *
 * @license     http://www.gnu.org/licenses/gpl-2.0.txt
 * @author      Mihály Kuprivecz <mkuprivecz@gmail.com>
 * @namespace   Beaver_Model
 */
class Default_Model_Filmx extends Beaver_Model {
	
	protected $_attributes = array (
		"id"               => null,
		"title"            => null,
		"titleAlternative" => null,
		"nation"           => null,
		"type"             => null,
		"year"             => null,
		"length"           => null,
		"message"          => null,
		"links"            => null,
		"scores"           => null,
		"movieRelease"     => null,
		"dvdRelease"       => null,
		"maker"            => null,
		"distributor"      => null,
		"shortDecription"  => null,
		"directors"        => null,
		"screenwriters"    => null,
		"producers"        => null,
		"actors"           => null,
		"pictures"         => null,
		"description"      => null,
		"externallinks"    => null,
	);
	
	protected $_attributesFromMapper = false;
	protected $_validatorsFromMapper = false;
	
	protected $_url  = "";
	
	protected $_html = "";
	
	protected $_positions = array();
	
	protected $_filmId = 1;
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function getFilmData( $start = null, $end = null, $save = null )
	{
		if ( null != $start && null != $end )
		{
			$data = array();
			for( $i = $start; $i <= $end; $i++ )
			{
				$this->setFilmId($i);
				$this->getData();
				//if( $save == true ) $this->save();				
				$data[] = $this->getAttributes();
				$this->clearAttributes();
			}
			return $data;
		}
		if ( null != $start && null == $end )
		{
			$this->setFilmId($start);
		}
		$this->getData();
		//if( $save == true ) $this->save();
		return $this->getAttributes();
	}
	
	public function saveFilmData( $id )
	{
		$this->setFilmId($start);
		$this->getData();
		 $this->save();
	}
	
	public function setFilmId( $id )
	{
		$this->_filmId = (int) $id;
	}
	
	public function getFilmId()
	{
		return $this->_filmId;
	}
	
	protected function getHtml()
	{
		$ch = curl_init();
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_PROXY, "http://proxy.westline.hu:3128");
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 (.NET CLR 3.5.30729) FirePHP/0.3");
		curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
		curl_setopt($ch, CURLOPT_USERPWD, "qpi:hurkale7");
		curl_setopt($ch, CURLOPT_PROXYUSERPWD, "westline+qpi:hurkale7"); 
		curl_setopt($ch, CURLOPT_URL, "http://filmindex.hu/movie/" . $this->getFilmId() );
		curl_setopt($ch, CURLOPT_HEADER, 0);
		
		$this->_html = curl_exec($ch);
		
		curl_close($ch);
	}
	
	protected function getData()
	{
		$this->getHtml();
		foreach ($this->_attributes as $name => $value )
		{
			$method = "parse" . ucfirst($name);
			$this->$method();
		}
	}
	
	protected function parseId()
	{
		$this->id = $this->getFilmId();
	}
	
	protected function parseTitle()
	{
		$start = strpos($this->_html,'<span id="focim">');
		$end   = strpos($this->_html,"</span>");
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 17;
			$this->title = substr($this->_html,$start,($end - $start) );
			$this->_positions["title"] = $end;
		}
		else
		{
			$this->title = "";
		}
	}
	
	protected function parseTitleAlternative()
	{
		$start = strpos($this->_html,'<span id="alcim">');
		$end   = strpos($this->_html,"</span>",$start);
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 17;
			$temp  = substr($this->_html, $start, ( $end - $start ) );
			if ( substr( $temp, 0, 1 ) == "(" && substr( $temp, -1, 1 ) == ")" )
			{
				$temp = substr($temp,1,-1);
			}
			$this->titleAlternative = $temp;
			$this->_positions["titleAlternative"] = $end;
		}
		else
		{
			$this->titleAlternative = '';
		}
	}
	
	protected function parseNation()
	{
		$start = strpos($this->_html,'<span class="kiemelt_szoveg">');
		$end   = strpos($this->_html,'</span>',$start);
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 29;
			$temp  = substr($this->_html,$start,($end - $start));
			$types = explode( ',', $temp );
			$types = array_map("trim",$types);
			//$this->nation = implode(",",$types);
			$this->nation = $types;
			$this->_positions["nation"] = $end + 6;
		}
	}
	
	protected function parseType()
	{
		if ( !isset($this->_positions["nation"]) ) return;
		$position = $this->_positions["nation"];
		strpos($this->_html,'<span class="kiemelt_szoveg">');
		$start = strpos($this->_html,'<span class="kiemelt_szoveg">', $position);
		$end   = strpos($this->_html,'</span>',$start);
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 29;
			$temp  = substr($this->_html,$start,($end - $start));
			$types = explode( ',', $temp );
			$types = array_map("trim",$types);
			//$this->type = implode(",",$types);
			$this->type = $types;
			$this->_positions["type"] = $end + 7;
		}
	}
	
	protected function parseYear()
	{
		if ( !isset($this->_positions["type"]) ) return;
		$start = $this->_positions["type"];
		$end   = strpos($this->_html,'<br/>',$start);
		if ( $start !== false  && $end !== false )
		{
			$temp = trim(substr($this->_html,$start,($end - $start)));
			if ( substr($temp,0,1) == "(" && substr($temp,-1,1) == ")" )
			{
				$temp = substr($temp,1,-1);
			}
			$this->year = $temp;
			$this->_positions["year"] = $end + 5;
		}
	}
	
	protected function parseLength()
	{
		if ( !isset($this->_positions["year"]) ) return;
		$start = strpos($this->_html,'<br/>', $this->_positions["year"]);
		$end   = strpos($this->_html,'perc', ( $start + 5 ) );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 5;
			$this->length = (int) trim(substr($this->_html,$start,($end - $start)));
			$this->_positions["length"] = $end;
		}
	}
	
	protected function parseMessage()
	{
		if ( !isset($this->_positions["length"]) ) return;
		$start = strpos($this->_html,'<br/>', $this->_positions["length"]);
		$end   = strpos($this->_html,'<br/>', ( $start + 5 ) );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 5;
			$this->message = trim(substr($this->_html,$start,($end - $start)));
			$this->_positions["message"] = $end;
		}
	}
	
	protected function parseLinks()
	{
		$data = array();
		$data["http"] = array();
		$data["trailer"] = array();
		$start = strpos($this->_html,'Hivatalos web');
		$end   = strpos($this->_html,'Felirat:', ( $start + 7 ) );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 18;
			$temp  = substr($this->_html,$start,($end - $start));
			$links = explode( '<a', $temp );
			$position = $end;
			foreach ($links as $link)
			{
				$start = strpos($link,'href="');
				$end   = strpos($link,'"', ( $start + 6 ) );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 6;
					$httplink = substr($link,$start,($end - $start));
					if( $httplink == "#")
					{
						$start = strpos($link,", '");
						$end   = strpos($link,"');", ( $start + 3 ) );
						if ( $start !== false  && $end !== false )
						{
							$start = $start + 3;
							$data["trailer"][] = substr($link,$start,($end - $start));
						}
					}
					else
					{
						$data["http"][] = $httplink; 
					}
				}
			}
			$this->links = $data;
		}
		$this->_positions["links"] = $end;
	}
	
	protected function parseScores()
	{
		$start = strpos($this->_html,'s:', $this->_positions["links"]  );
		$end   = strpos($this->_html,'<br/>', ( $start + 1 ) );
		$this->scores = array();
		if ( $start !== false  && $end !== false )
		{
			$position = $end;
			$start = $start + 5;
			$list  = trim(substr($this->_html,$start,($end - $start)));
			$data  = array();
			if (strpos($list,":") !== false)
			{
				$list  = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $list);
				$list  = str_replace("\t","",$list);
				$scores = explode("\n",$list);
				foreach ($scores as $score)
				{
					$temp = explode( ":", $score );
					$temp = array_map( "trim", $temp );
					$data[$temp[0]] = $temp[1];
				}
			}
			$this->scores = $data;
		}
	}
	
	protected function parseMovieRelease()
	{
		$start = strpos($this->_html,'mozi premier:');
		$end   = strpos($this->_html,'<br/>', ( $start + 12 ) );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 14;
			$end   = $end - 2;
			$date  = substr($this->_html,$start,($end - $start));
			$date  = str_replace(".","-",$date);
			$this->movieRelease = $date;
		}
	}
	
	protected function parseDvdRelease()
	{
		$start = strpos($this->_html,'DVD premier:');
		$end   = strpos($this->_html,'<br/>', ( $start + 11 ) );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 13;
			$end   = $end - 2;
			$position = $end;
			$date  = substr($this->_html,$start,($end - $start));
			$date  = str_replace(".","-",$date);
			$this->dvdRelease = $date;
			$this->_positions["dvdRelease"] = $end;
		}
	}
	
	protected function parseMaker()
	{
		if ( !isset($this->_positions["dvdRelease"]) ) return;
		$start = $this->_positions["dvdRelease"] + 10;
		$end   = strpos($this->_html,'<br/>', $start );
		if ( $start !== false  && $end !== false )
		{
			$temp = explode(",", trim(substr($this->_html,$start,($end - $start))));
			$temp = array_map("trim",$temp);
			foreach ($temp as $index => $element) {

				$tempData = explode(":",$element);
				if ( isset($tempData[1]) )
				{
					switch ($index)
					{
						case 0:
							$this->maker = trim($tempData[1]);
							break;
						case 1:
							$this->distributor = trim($tempData[1]);
							break;
					}
				}
			}
			$this->_positions["maker"] = $end;
			$position = $end;
		}
	}
	
	protected function parseDistributor()
	{
		//<<< setted in previous method
	}
	
	protected function parseShortDecription()
	{
		if ( !isset($this->_positions["maker"]) ) return;
		$start = strpos($this->_html,'<span class="kiemelt_szoveg">', $this->_positions["maker"] );
		$end   = strpos($this->_html,'</span>', $start );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 29;
			$this->shortDecription = trim(substr($this->_html,$start,($end - $start)));
		}
	}
	
	protected function parseDirectors()
	{
		$positon = strpos($this->_html,'<a href="#" class="top_link" id="alkotok">');
		$start = strpos($this->_html,'Rendez', $positon );
		$end   = strpos($this->_html,'<br/>', $start );
		if ( $start !== false  && $end !== false )
		{
			$temp  = explode(">,", trim(substr($this->_html,$start,($end - $start))) );
			$data = array();
			foreach ($temp as $html)
			{
				$start = strpos($html,'<span class="szemelynev">' );
				$end   = strpos($html,'</span', $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 25;
					$name  = trim(substr($html,$start,($end - $start)));
				}
				$start = strpos($html,'/creative/' );
				$end   = strpos($html,"'", $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 10;
					$id    = trim(substr($html,$start,($end - $start)));
					if (is_numeric($id))
					{
						$id = (int) $id;
					}
					else
					{
						$id = "";
					}
				}
				$data[] = array( "id" => $id, "name" => $name );
			}
			$this->directors = $data;
		}
		$this->_positions["directors"] = $end;
	}
	
	protected function parseScreenwriters()
	{
		if ( !isset($this->_positions["directors"]) ) return;
		$start = strpos($this->_html,'Forgat', $this->_positions["directors"] );
		$end   = strpos($this->_html,'<br/>', $start );
		if ( $start !== false  && $end !== false )
		{
			//$start = + 2;
			$temp  = explode(">,", trim(substr($this->_html,$start,($end - $start))) );
			$data = array();
			foreach ($temp as $html)
			{
				$start = strpos($html,'<span class="szemelynev">' );
				$end   = strpos($html,'</span', $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 25;
					$name  = trim(substr($html,$start,($end - $start)));
				}
				$start = strpos($html,'/creative/' );
				$end   = strpos($html,"'", $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 10;
					$id    = trim(substr($html,$start,($end - $start)));
					if (is_numeric($id))
					{
						$id = (int) $id;
					}
					else
					{
						$id = "";
					}
				}
				$data[] = array( "id" => $id, "name" => $name );
			}
			$this->screenwriters = $data;
		}
		$this->_positions["screenwriters"] = $end;
	}
	
	protected function parseProducers()
	{
		if ( !isset($this->_positions["screenwriters"]) ) return;
		$start = strpos($this->_html,'Producer', $this->_positions["screenwriters"] );
		$end   = strpos($this->_html,'<br/>', $start );
		if ( $start !== false  && $end !== false )
		{
			$temp  = explode(">,", trim(substr($this->_html,$start,($end - $start))) );
			$data = array();
			foreach ($temp as $html)
			{
				$start = strpos($html,'<span class="szemelynev">' );
				$end   = strpos($html,'</span', $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 25;
					$name  = trim(substr($html,$start,($end - $start)));
				}
				$start = strpos($html,'/creative/' );
				$end   = strpos($html,"'", $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 10;
					$id    = trim(substr($html,$start,($end - $start)));
					if (is_numeric($id))
					{
						$id = (int) $id;
					}
					else
					{
						$id = "";
					}
				}
				$data[] = array( "id" => $id, "name" => $name );
			}
			$this->producers = $data;
		}
		$this->_positions["producers"] = $end;
	}
	
	protected function parseActors()
	{
		$start = strpos($this->_html,'<div id="szereplo_lista">');
		$end   = strpos($this->_html,'</div>', $start );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 25;
			$temp  = explode("<br />",trim(substr($this->_html,$start,($end - $start))));
			$temp  = array_map("trim",$temp); 
			$data  = array();
			//var_dump($temp);
			foreach ($temp as $html)
			{
				if ( strlen($html) < 5 ) continue;
				$start = strpos($html,'/creative/' );
				$end   = strpos($html,"'", $start );
				$name = "";
				$id   = "";
				$filmName = "";
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 10;
					$id    = trim(substr($html,$start,($end - $start)));
					if (is_numeric($id))
					{
						$id = (int) $id;
					}
					else
					{
						$id = "";
					}
				}
				$start = strpos($html,'class="szemelynev">' );
				$end   = strpos($html,"</a>", $start );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 19;
					$name    = trim(substr($html,$start,($end - $start)));
				}
				$mainCharacter = false;
				if ( strpos($html,"font-weight: bold") !== false )
				{
					$mainCharacter = true;
				}
				$position = strpos($html,'<span' );
				$start = strpos($html,'>', $position );
				$end   = strpos($html,"</span", $start );
				$filmName = "";
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 1;
					$filmName = trim(substr($html,$start,($end - $start)));
				}
				
				$data[] = array( "id" => $id, "name" => $name, "mainCharceter" => $mainCharacter, "filmName" => $filmName );
			}
			$this->actors = $data;
		}
	}
	
	protected function parsePictures()
	{
		$position = strpos($this->_html,'<a href="#" class="top_link" id="kepek">');
		$start = strpos($this->_html,'<div class="kepkeret">');
		$end   = strpos($this->_html,'</td>', $start );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 22;
			$temp = explode('<div class="kepkeret">',trim(substr($this->_html,$start,($end - $start))));
			$temp  = array_map("trim",$temp);
			$data  = array();
			foreach ($temp as $html)
			{
				$start = strpos($html,'href="' );
				$end   = strpos($html,'" target', $start );
				$imageUrl = "";
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 6;
					$imageUrl = trim(substr($html,$start,($end - $start)));
				}
				$start = strpos($html,'<br/>' );
				$end   = strpos($html,'</div>', $start );
				$actors = array();
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 5;
					$links = explode(">,",trim(substr($html,$start,($end - $start))));
					$links  = array_map("trim",$links);
					foreach ($links as $link)
					{
						$start = strpos($html,'/creative/' );
						$end   = strpos($html,"'", $start );
						if ( $start !== false  && $end !== false )
						{
							$start = $start + 10;
							$actors[] = (int) trim(substr($html,$start,($end - $start)));
						}
					}
					
				}
				$data[] = array( "url" => $imageUrl, "actors" => $actors );
			}
			$this->pictures = $data;
		}
	}
	
	protected function parseDescription()
	{
		$start = strpos($this->_html,'id="bovebben_leiras"');
		$end   = strpos($this->_html,'</td>', $start );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 46;
			$this->description = trim( substr($this->_html,$start,($end - $start)));
		}
	}
	
	protected function parseExternallinks()
	{
		$position = strpos($this->_html,'id="linkek"');
		$start = strpos($this->_html,'class="cella_alul_keret">',$position);
		$end   = strpos($this->_html,'<br/>', $start );
		if ( $start !== false  && $end !== false )
		{
			$start = $start + 25;
			$temp  = explode("</a>,", trim(substr($this->_html,$start,($end - $start) ) ) );
			$temp  = array_map("trim",$temp);
			$links = array();
			foreach ($temp as $html)
			{
				if ( strlen($html) < 5 ) continue;
				$name = trim( substr( $html, ( strpos($html,'>') + 1 ) ) );
				if ( strpos($name,"imdb") !== false ) $name = "imdb";
				$start =  strpos($html,'"');
				$end   =  strpos($html,'"', ( $start + 1 ) );
				if ( $start !== false  && $end !== false )
				{
					$start = $start + 1;
					$links[$name] = trim( substr($html,$start,($end - $start))); 
				}
			}
			$this->externallinks = $links;
		}
	}
}
